﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using BaseCardTypes = Wormhole.Engine.CardLibrary.BaseCardTypes;
using CardTypes = Wormhole.Engine.CardLibrary.CardTypes;

namespace Wormhole.UI.Application.Controls.Mission
{
	/// <author>Hannes Foulds</author>
	/// <date>20 June 2008</date>
	/// <summary>
	/// Display the failed missions.
	/// </summary>
	public class FailedMissions : Layout.Cards
	{
		#region Constants
		/// <summary>The space between cards.</summary>
		public const int SPACER = 20;
		#endregion

		#region Set Player Events
		/// <summary>
		/// Wire up events for the player object.
		/// </summary>
		protected override void SetPlayerEvents()
		{
			this.Player.Game.Mission.MissionFailed += new Wormhole.Engine.Mission.Mission.MissionFailedDelegate(Mission_MissionFailed);
			this.Player.Game.Mission.FailedMissions.CardRemoved += new Wormhole.Engine.Decks.Deck.CardRemovedDelegate(FailedMissions_CardRemoved);
		}
		#endregion

		#region Create Card
		/// <summary>
		/// Create the UI card that will be rendered.
		/// </summary>
		/// <param name="card">The engine card to create a control for.</param>
		/// <returns>Returns the UI control that will display the card.</returns>
		protected override Cards.Card CreateCard(BaseCardTypes.Card card)
		{
			return new FailedMission(card);
		}
		#endregion

		#region Event Handlers

		#region Card Click
		/// <summary>
		/// Handles a single click on a card.
		/// </summary>
		/// <param name="card">The card that was clicked.</param>
		protected override void OnCardClick(Wormhole.UI.Application.Controls.Cards.Card card)
		{
			this.DisplayCardDetails(card);
		}
		#endregion

		#region Card Double Click
		/// <summary>
		/// Handle a double click on a card.
		/// </summary>
		/// <param name="card">The card that was clicked</param>
		protected override void OnCardDoubleClick(Cards.Card card)
		{
		}
		#endregion

		#region Mission Failed
		/// <summary>
		/// Handle the mission failure event.
		/// </summary>
		/// <param name="mission">The mission that was failed.</param>
		private void Mission_MissionFailed(Wormhole.Engine.CardLibrary.CardTypes.Mission mission)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.AddCard(mission);
			});
		}
		#endregion

		#region Mission Removed
		/// <summary>
		/// Handle the event when a failed mission is placed back in the mission pile.
		/// </summary>
		/// <param name="card">The mission card.</param>
		private void FailedMissions_CardRemoved(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.RemoveCard(card);
			});
		}
		#endregion

		#endregion

		#region Layout
		/// <summary>
		/// Layout the cards.
		/// </summary>
		protected override void Layout()
		{
			int top = 0;
			if (this.cards.Count > 0)
			{
				// position the buttons
				foreach (Controls.Cards.Card card in this.cards)
				{
					card.Margin = new Thickness(0, top, 0, 0);
					card.HorizontalAlignment = HorizontalAlignment.Left;
					card.VerticalAlignment = VerticalAlignment.Top;
					top += SPACER;
				}
			}
		}
		#endregion
	}
}
